<template>
  <div>
    <Navbar :title="title" left-text="返回" left-arrow @click-left="goBack"></Navbar>

    <van-address-edit :area-list="areaList" show-set-default :address-info="address"
      :area-columns-placeholder="['请选择', '请选择', '请选择']" @save="onSave" />
  </div>
</template>

<script>
import Navbar from "../Navbar.vue";
import { areaList } from "@vant/area-data";

export default {
  name: "AddressAdd",
  data() {
    return {
      title: "添加收货地址",
      areaList,
      user: this.$cookies.get("ShopUser"),
      action: this.$route.query.action ? this.$route.query.action : 'user',
      idArr: this.$route.query.idArr ? this.$route.query.idArr : 0,
    };
  },
  methods: {
    async onSave(val) {
      let data = {
        consignee: val.name,
        mobile: val.tel,
        code: val.areaCode,
        address: val.addressDetail,
        status: val.isDefault ? 1 : 0,
        userid: this.user.id,
      };

      let res = await this.$api.add(data);

      if (res.code === 1) {
        this.$notify({
          type: "success",
          message: res.msg,
          duration: 1500,
          onClose: () => {
            if (this.action === 'user') {
              this.$router.push({ path: res.url, query: { action: this.action } });
            } else if (this.idArr != null) {
              this.$router.push({ name: 'AddressBase', query: { action: this.action, nums: this.nums, idArr: this.idArr } });
            } else {
              console.log(-1);
              this.$router.go(-1)
            }
          },
        });
        return false;
      } else {
        this.$notify({
          type: "danger",
          message: res.msg,
          duration: 1500,
        });
        return false;
      }
    },
    goBack() {
      if (this.action === 'user') {
        this.$router.push({ name: 'AddressBase', params: { action: this.action } });
      } else {
        this.$router.push({ name: 'AddressBase', query: { action: this.action, nums: this.nums, idArr: this.idArr } });
      }
      // this.$router.push('/address/base/index');
    },
  },
  created() { },
  computed: {
    address() { },
  },
  components: {
    Navbar,
  },
};
</script>

<style scoped>
</style>
